<!-- 按户统计 -->
<div class="modal fade" id="create-hu-modal">
	<div class="modal-dialog" role="document">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
						aria-hidden="true">×</span></button>
				<span style="font-size: 20px;"><span id="referralname">添加数据</span></span>
			</div>
			<div class="modal-body">
				<div data-bind="visible: loading" class="loading-panel" style="display: none;">
					<i class="fa fa-spin fa-spinner"></i>
				</div>
				<form class="form-horizontal modelform">
					<div class="row">
						<div class="col-md-6">
							<div class="form-group">
								<label class="control-label col-sm-3"><span class="required" style="color: red">*</span>公众号</label>
								<div class="col-lg-8">
									<select name="operate" id="operate" class="selectpicker multipleSelete" data-live-search="true">
										{foreach name="gzhList" item="vo" key="k" }
										<option value="{$k}" {if input('get.operate')==$k}selected {/if}>{$vo}</option>
										{/foreach}
									</select>
								</div>
							</div>
						</div>
						<div class="col-md-6">
							<div class="form-group">
								<label class="control-label col-sm-3"><span class="required" style="color: red">*</span>户</label>
								<div class="col-lg-8">
									<input type="text" class="form-control" name="family">
								</div>
							</div>
						</div>
						<div class="col-md-6">
							<div class="form-group">
								<label class="control-label col-sm-3"><span class="required" style="color: red">*</span>书名</label>
								<div class="col-lg-8">
									<select name="book_id" id="book_id" class="selectpicker multipleSelete" data-live-search="true">
										{foreach name="book" item="vo" key="k" }
										<option value="{$k}" {if input('get.book_id')==$k}selected {/if}>{$vo.title}</option>
										{/foreach}
									</select>
								</div>
							</div>
						</div>
						<div class="col-md-6">
							<div class="form-group">
								<label class="control-label col-sm-3"><span class="required" style="color: red">*</span>推广ID</label>
								<div class="col-lg-8">
									<input type="text" class="form-control" name="extension_id"  placeholder="多个id请用逗号分割">
								</div>
							</div>
						</div>
						<div class="col-md-6">
							<div class="form-group">
								<label class="control-label col-sm-3"><span class="required" style="color: red">*</span>消耗</label>
								<div class="col-lg-8">
									<input type="text" class="form-control" name="consume">
								</div>
							</div>
						</div>
						<div class="col-md-6">
							<div class="form-group">
								<label class="control-label col-sm-3">单点</label>
								<div class="col-lg-8">
									<input type="text" class="form-control" name="point">
								</div>
							</div>
						</div>
						<div class="col-md-6">
							<div class="form-group">
								<label class="control-label col-sm-3">调起成本</label>
								<div class="col-lg-8">
									<input type="text" class="form-control" name="cost">
								</div>
							</div>
						</div>
						<div class="col-md-6">
							<div class="form-group">
								<label class="control-label col-sm-3">粉价</label></label>
								<div class="col-lg-8">
									<input type="text" class="form-control" name="price">
								</div>
							</div>
						</div>
						<div class="col-md-6">
							<div class="form-group">
								<label class="control-label col-sm-3">付费率</label>
								<div class="col-lg-8">
									<input type="text" class="form-control" name="rate">
								</div>
							</div>
						</div>
						<div class="col-md-6">
							<div class="form-group">
								<label class="control-label col-sm-3"><span class="required" style="color: red">*</span>推广日</label>
								<div class="col-lg-8">
									<input type="text" class="form-control" autocomplete="off" name="extension_date" id="extension_date" placeholder="年-月-日">
								</div>
							</div>
						</div>
					</div>
					<input type="hidden" name="id">
					<input type="hidden" name="refresh">
				</form>
			</div>
			<div class="modal-footer">
				<button type="button" id="createreferral_hu" class="btn btn-primary ">提交</button>
			</div>
		</div>
	</div>
</div>
<script>
    // 新增
	$("#createreferral_hu").on('click',function(){
		
		var data = $('.modelform').serializeJson();
		if (!$("select[name=operate]").selectpicker('val')) {
			updateAlert("公众号不能为空", 'danger');
			return false;
		}
		let params = ['family','book_id','extension_id','consume','extension_date']
		for (var i = 0; i < params.length; i++){
			if(!data[params[i]]){
				updateAlert("必填参数不能为空", 'danger');
				return false;
			}
		}
		$(this).attr('disabled',true)
		Ajax(url, data).then(result => {
			if(result.code==1){
				updateAlert(result.msg, 'success');
				setTimeout(function() {
					window.location.reload();
				}, 100);
			}else{
				updateAlert("添加"+result.msg, 'danger');
			}
		});
	});
    
    //编辑功能
	function editreferral_hu(ee,modelName,type){
		if(type == 'refresh'){
			url = "{:url('refresh')}"
			$("#create-hu-modal #referralname").html("更新数据");
		}else{
			url = "{:url('edit')}"
			$("#create-hu-modal #referralname").html("修改数据");
		}

		var is_rule = '{:checkAuthRule("admin/statistics/edit",false)}';
		if(!is_rule && rule!='close_rule'){
			layer.msg("你没权限编辑");
			return;
		}

		$("#batchtxt").remove();
		var jsonStr = $(ee).attr('data-json');

		var $modal = $('#'+modelName);
		$modal.on('show.bs.modal', function () {

			var json =  eval("(" + jsonStr + ")");

			let keys = Object.keys(json)
			for (var i = 0; i < keys.length; i++){
				$(`input[name=${keys[i]}]`).val(json[keys[i]])
			}
			if(modelName == 'create-hu-modal'){
				$("select[name=operate]").selectpicker('val', [json.official_account_id]);
				$("select[name=book_id]").selectpicker('val', [json.book_id]);
				$('#extension_date').fdatepicker({
					format: 'yyyy-mm-dd',
					initialDate:json.extension_date
				});
			}
			
		});

		$modal.modal({backdrop: 'static', keyboard: false})
	}

	$('#time').fdatepicker({
		format: 'yyyy-mm-dd',
	});

	$('#refresh').click(function(){
		let id = []
		$('.tFamilyBody tr').each((index,value)=>{
			id.push($(value).data('id'))
		})
		$.ajax({
			type: "POST",
			url: "{:url('statistics/updateData')}",
			data:{
				id:id.toString()
			},
			dataType: "json",
			success: function(data){
				if(data.code==1){
					updateAlert(data.msg, 'success');
					setTimeout(function() {
						window.location.reload();
					}, 100);
				}
			}
		})
	})

	$('#refreshRoi').click(function(){
		let id = []
		$('.tFamilyBody tr').each((index,value)=>{
			id.push($(value).data('id'))
		})
		$.ajax({
			type: "POST",
			url: "{:url('statistics/refreshRoi')}",
			data:{
				id:id.toString()
			},
			dataType: "json",
			success: function(data){
				if(data.code==1){
					updateAlert(data.msg, 'success');
					setTimeout(function() {
						window.location.reload();
					}, 100);
				}
			}
		})
	})
</script>